<?php
require 'Model.php';
class IndexModel extends Model{
	function __construct(){
    parent::__construct();
	}
	function getIndex(){
        $sql="select * from cartoons";
        $data=$this->conn->query($sql);
		return $data;
	}
	function getlistall($tid){

		$sql="select * from cartoons where tid=$tid";
		        $data=$this->conn->query($sql);
		        return $data;
	}
	function getshow($cid){
		$sql="select * from cartoons where cid=$cid";
		$sqll="update cartoons set hittimes=hittimes+1 where cid=$cid";
	    $data=$this->conn->query($sql);
	    $this->conn->query($sqll);
		        return $data;
	}

               function comment($uname)
            {
              $sql1="select * from users where uname='$uname' ";
              $data=$this->conn->query($sql1);
              $uid=$data['uid'];
              $cid=$_POST["cid"];
              $content=$_POST["content"];
              $date=date("YmdHis");
              $sql = "insert into comments values(null,'$content','$date','$uid','$cid')";
              $result=$this->conn->query($sql);
              return array($data,$result);
            }
            function getcommentlist($uid){
               $sql="select * from comments where uid=$uid";
              $data=$this->conn->query($sql);
              return $data;
            }

	function dologin($data){
		$uname=trim($data['uname']);
    	$password=$data['password'];
    	$sql="select * from users where uname='$uname' and password=md5('password') ";

    	$result=$this->conn->query($sql);
        $_SESSION["uid"]=$result['uid'];
        @setcookie("uid",$data['uid'],time()+3600*24);
        echo $_SESSION["uid"];
		return $result;
    
	}
  function adminlist($data){
      $adminname=trim($data['adminname']);
      $password=$data['password'];
      $sql="select * from admins where adminname='$adminname' and password=md5('password') ";
      $result=$this->conn->query($sql);
    return $result;

  }
    function addUser($data){
        if(!empty($_FILES["pic"]["name"])){
            $pic = $this->uploadfile('pic','./upload/',array('jpg','gif','png'));
        }
        else{
            $pic='';
        }

        $user = array(
            'uname' => trim($data['uname']),
            'password' => $data['password'],
            'gender' => $data['gender'],
            'birthdate' => $data['birthdate'],
            'hobby' => implode($data['hobby'], ','),
            'degree' => $data['degree'],
            'intro' => $data['intro'],
            'pic'=>$pic
        );
        $keys = implode(',',array_keys($user));
        $values = implode("','", array_values($user));
        $sql="insert into users ({$keys}) VALUES ('{$values}')";
        //执行SQL语句
        $result = $this->conn->query($sql);
        return $result;

    }
//	function adduser($data,$file)
//            {
//                  $uname=$data['username'];
//                  $gender=$data['gender'];
//                  $birthday=$data['birthdate'];
//                  $hobbyarray=$data['hobby'];
//                  $hobby="";
//                 for($i=0;$i<count($hobbyarray);$i++)
//                  {    $hobby.=$hobbyarray[$i];
//                   if($i<count($hobbyarray)-1)
//                  $hobby.=',';
//                   }
//                //$hobby=implode($data[hobby],',');
//               $degree=$data['degree'];
//              $intro=$data['intro'];
//               $pic=$data['file'];
//
//             //print_r($_FILES[pic]);
//
//           $pic=$this->uploadfile($pic);
//$sql="insert into users values(null,'$uname',md5('password'),0,'$gender','$birthday','$hobby',$degree ,'$intro','$pic')";
//            $num=$this->conn->query($sql);
//              return $num;
//            }

            function getshowuser($uname){
              $sql="select * from users where uname='$uname'";
              $data=$this->conn->query($sql);
              return $data;
            }
            function search($cartoonname){
             $key=trim($_POST['key']);
             $sql="select * from cartoons  where (cartoonname like'%$key%')";
             $data=$this->conn->query($sql);
             return $data;
            }
                       function edituser($uid){
          $sql="select * from users where uid=$uid";
          $data=$this->conn->query($sql);
          return $data; 
        }
        function updateuser($data,$file){
        $uid=$data['uid'];
        $username=$data['username'];
        $gender=$data['gender'];
        $birthdate=$data['birthdate'];
        $hobby=implode($data['hobby'],',');
        $degree=$data['degree'];
        $intro=$data['intro'];
    //接收数据
    if($file['pic']['size']==0){
      //没上传新头像
      $sql="update users set uname='$username',birthdate='$birthdate',gender=$gender,hobby='$hobby',degree=$degree,intro='$intro' where uid=$uid ";
    }
    else{
        $arr=explode('.',$file['pic']['name']);
    $suffix=$arry[count($arr)-1];//后缀名
    $newname=date("YmdHis").rand(100,999).'.'.$suffix;//文件新名字
    $filepath='./posters/';    move_uploaded_file($file['pic']['tmp_name'], $filepath.$newname);
    $sql="update users set uname='$username',birthdate='$birthdate',gender=$gender,hobby='$hobby',degree=$degree,intro='$intro',pic='$newname' where uid=$uid ";
      //上传头像并且根据uid更新数据
    }
    
//如果修改了头像，需要上传新头像，否则原头像不动
       $result=$this->conn->query($sql);
    return $result;
    
  }
}?>